package controllerGoods

import (
	"github.com/gin-gonic/gin"
	doGoods "mxshop/app/mxshop_api/api/internal/domain/do/goods"
	dtoGoods "mxshop/app/mxshop_api/api/internal/domain/dto/goods"
	"mxshop/gmicro/pkg/common/core"
	"mxshop/pkg/translator/gin"
	"net/http"
	"strconv"
)

type GoodsForm struct {
	Name        string   `form:"name" json:"name" binding:"required,min=2,max=100"`
	GoodsSn     string   `form:"goods_sn" json:"goods_sn" binding:"required,min=2,lt=20"`
	Stocks      int32    `form:"stocks" json:"stocks" binding:"required,min=1"`
	MarketPrice float32  `form:"market_price" json:"market_price" binding:"required,min=0"`
	ShopPrice   float32  `form:"shop_price" json:"shop_price" binding:"required,min=0"`
	GoodsBrief  string   `form:"goods_brief" json:"goods_brief" binding:"required,min=3"`
	Images      []string `form:"images" json:"images" binding:"required,min=1"`
	DescImages  []string `form:"desc_images" json:"desc_images" binding:"required,min=1"`
	ShipFree    *bool    `form:"ship_free" json:"ship_free" binding:"required"`
	FrontImage  string   `form:"front_image" json:"front_image" binding:"required,url"`
	CategoryId  int64    `form:"category" json:"category" binding:"required"`
	BrandId     int64    `form:"brand" json:"brand" binding:"required"`
}

type GoodsStatusForm struct {
	IsNew  *bool `form:"new" json:"new" binding:"required"`
	IsHot  *bool `form:"hot" json:"hot" binding:"required"`
	OnSale *bool `form:"sale" json:"sale" binding:"required"`
}

func (gc *GoodsController) GoodsList(ctx *gin.Context) {
	var r dtoGoods.GoodsFilter

	if err := ctx.ShouldBindQuery(&r); err != nil {
		translatorGin.HandleValidatorError(ctx, err, gc.trans)
		return
	}

	ret, err := gc.srv.Goods().Goods().GoodsList(ctx, &r)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	ctx.JSON(http.StatusOK, ret)
	return
}

func (gc *GoodsController) GoodsNew(ctx *gin.Context) {
	goodsForm := GoodsForm{}
	// 如果前端传递的是 form 只需要改成 ctx.ShouldBind(&goodsForm) 即可
	if err := ctx.ShouldBindJSON(&goodsForm); err != nil {
		translatorGin.HandleValidatorError(ctx, err, gc.trans)
		return
	}

	rsp, err := gc.srv.Goods().Goods().CreateGoods(ctx, &dtoGoods.GoodsDTO{
		GoodDO: doGoods.GoodDO{
			Name:            goodsForm.Name,
			GoodsSn:         goodsForm.GoodsSn,
			Stocks:          goodsForm.Stocks,
			MarketPrice:     goodsForm.MarketPrice,
			ShopPrice:       goodsForm.ShopPrice,
			GoodsBrief:      goodsForm.GoodsBrief,
			ShipFree:        *goodsForm.ShipFree,
			Images:          goodsForm.Images,
			DescImages:      goodsForm.DescImages,
			GoodsFrontImage: goodsForm.FrontImage,
			Category:        &doGoods.CategoryDO{Id: goodsForm.CategoryId},
			Brand:           &doGoods.BrandDO{Id: goodsForm.BrandId},
		},
	})
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}

	ctx.JSON(http.StatusOK, rsp)
	return
}

func (gc *GoodsController) GoodsDetail(ctx *gin.Context) {
	id := ctx.Param("id")
	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}

	ret, err := gc.srv.Goods().Goods().GetGoodsDetail(ctx, idInt)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	ctx.JSON(http.StatusOK, ret)
	return
}

func (gc *GoodsController) GoodsDelete(ctx *gin.Context) {
	id := ctx.Param("id")
	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}

	if err := gc.srv.Goods().Goods().DeleteGoods(ctx, idInt); err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{})
	return
}

func (gc *GoodsController) GoodsStocks(ctx *gin.Context) {
	id := ctx.Param("id")
	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}

	ret, err := gc.srv.Inv().Inv().GetInvDetail(ctx, idInt)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	ctx.JSON(http.StatusOK, ret)
	return
}

func (gc *GoodsController) GoodsUpdate(ctx *gin.Context) {
	goodsForm := GoodsForm{}
	// 如果前端传递的是 form 只需要改成 ctx.ShouldBind(&goodsForm) 即可
	if err := ctx.ShouldBindJSON(&goodsForm); err != nil {
		translatorGin.HandleValidatorError(ctx, err, gc.trans)
		return
	}

	id := ctx.Param("id")
	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}

	if err := gc.srv.Goods().Goods().UpdateGoods(ctx, &dtoGoods.GoodsDTO{
		GoodDO: doGoods.GoodDO{
			Id:              idInt,
			Name:            goodsForm.Name,
			GoodsSn:         goodsForm.GoodsSn,
			Stocks:          goodsForm.Stocks,
			MarketPrice:     goodsForm.MarketPrice,
			ShopPrice:       goodsForm.ShopPrice,
			GoodsBrief:      goodsForm.GoodsBrief,
			ShipFree:        *goodsForm.ShipFree,
			Images:          goodsForm.Images,
			DescImages:      goodsForm.DescImages,
			GoodsFrontImage: goodsForm.FrontImage,
			Category:        &doGoods.CategoryDO{Id: goodsForm.CategoryId},
			Brand:           &doGoods.BrandDO{Id: goodsForm.BrandId},
		},
	}); err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": "更新成功",
	})
	return
}

func (gc *GoodsController) GoodsUpdateStatus(ctx *gin.Context) {
	goodsStatusForm := GoodsStatusForm{}
	if err := ctx.ShouldBindJSON(&goodsStatusForm); err != nil {
		translatorGin.HandleValidatorError(ctx, err, gc.trans)
		return
	}

	id := ctx.Param("id")
	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	err = gc.srv.Goods().Goods().UpdateGoods(ctx, &dtoGoods.GoodsDTO{
		GoodDO: doGoods.GoodDO{
			Id:     idInt,
			IsNew:  *goodsStatusForm.IsNew,
			IsHot:  *goodsStatusForm.IsHot,
			OnSale: *goodsStatusForm.OnSale,
		},
	})
	if err != nil {
		core.WriteResponse(ctx, err, nil)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": "修改成功",
	})
}
